package mainpackage;
import javax.swing.JOptionPane;

import school.Classroom;
import school.Students;
public class Main {
	public static ConsoleFrame console = new ConsoleFrame("School");
	public static void main(String args[])
	{
		// HLO bhleen
		//declaring new class
		Classroom class1 = new Classroom();
		console.setVisible(true);
		//cout this sentence
		console.log("entre students num");
		//this index is for casting from string to int
		String index;
		index = JOptionPane.showInputDialog("please entre students num");
		class1.studentsNum=Integer.parseInt(index);
		console.log("num of students is " + class1.studentsNum);
		class1.avgs=0;
		class1.student=new Students [class1.studentsNum+1];
		index = JOptionPane.showInputDialog("please entre class num");
		class1.number=Integer.parseInt(index);
		console.log("class num is " + class1.number);
		index = JOptionPane.showInputDialog("please entre teahcers id");
		class1.teacher.id=Integer.parseInt(index);
		console.log("id of teacher is " + class1.teacher.id);
		class1.teacher.firstName = JOptionPane.showInputDialog("please entre teachers first name");
		class1.teacher.lastName = JOptionPane.showInputDialog("please entre teachers last name");
		console.log("teachers name is " + class1.teacher.firstName + " " + class1.teacher.lastName);
		//declaring array elements
		//====Eyad
		// After allocating an array with new wou can use array.length
		for (int i = 0; i < class1.student.length; i++) 
		{
			class1.student[i]=new Students();
		}
		//read students infos
		for (int i = 0; i < class1.student.length; i++)
		{
			index = JOptionPane.showInputDialog("please entre id of student num  " + (i+1));
			class1.student[i].Id=Integer.parseInt(index);
			console.log("students " + i +"id is" + class1.student[i].Id);
			class1.student[i].firstName = JOptionPane.showInputDialog("enter students num " + (i+1) +" first name");
			class1.student[i].lastName = JOptionPane.showInputDialog("enter students num " + (i+1) +" last name");
		}
		//read students grades
		for (int i = 0; i < class1.student.length; i++)
		{
			index=JOptionPane.showInputDialog("please entre the math grade of" + class1.student[i].firstName + " " +class1.student[i].lastName);
			class1.student[i].math=Integer.parseInt(index);
			class1.student[i].avg+=class1.student[i].math;
			index=JOptionPane.showInputDialog("please entre the english grade of" + class1.student[i].firstName + " " +class1.student[i].lastName);
			class1.student[i].english=Integer.parseInt(index);
			class1.student[i].avg+=class1.student[i].english;
			index=JOptionPane.showInputDialog("please entre the phisics grade of" + class1.student[i].firstName + " " +class1.student[i].lastName);
			class1.student[i].phisics=Integer.parseInt(index);
			class1.student[i].avg+=class1.student[i].phisics;
			index=JOptionPane.showInputDialog("please entre the bio grade of" + class1.student[i].firstName + " " +class1.student[i].lastName);
			class1.student[i].bio=Integer.parseInt(index);
			class1.student[i].avg+=class1.student[i].bio;
			class1.student[i].avg/=4;
			class1.avgs+=class1.student[i].avg;
		}
		//sort the students by their marks
		for(int i = 0;i<class1.student.length; i++)
		{
			for(int q=i+1;q<class1.student.length;q++)
			{
				if(class1.student[q].avg>class1.student[i].avg)
				{
					class1.student[class1.studentsNum]=class1.student[q];
					class1.student[q]=class1.student[i];
					class1.student[i]=class1.student[class1.studentsNum];
				}
			}
		}
		//display the average of all students
		for (int i = 0; i <class1.student.length; i++)
		{
			console.log("the average of"+ class1.student[i].firstName + " " + class1.student[i].lastName + " is " + class1.student[i].avg);
		}
		class1.avgs/=class1.studentsNum;
		//display the average of averages
		console.log("the average of alll students is"+ class1.avgs);
		//display the best student
		console.log("the best student is"+ class1.student[0].firstName + " " + class1.student[0].lastName);
		//display the worst student
		console.log("the worst student is"+ class1.student[class1.studentsNum-1].firstName + " " + class1.student[class1.studentsNum-1].lastName);
		//console.log("You entered:"+ name);//yes
		
		class1.print();
	}
} 
